package com.example.securityconfig.config;


import com.example.securityconfig.filter.JwtSecurityFilter;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@Log4j2
// 开启security权限配置
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    // 认证失败处理器
    private final SecurityAuthenticationEntryPointExceptionHandler securityAuthenticationEntryPointExceptionHandler;

    // 权限校验处理器
    private final SecurityAccessDeniedHandlerExceptionHandler securityAccessDeniedHandlerExceptionHandler;

    //自定义jwt过滤器
    private final JwtSecurityFilter jwtSecurityFilter;
    public SecurityConfig(JwtSecurityFilter jwtSecurityFilter, SecurityAuthenticationEntryPointExceptionHandler securityAuthenticationEntryPointExceptionHandler, SecurityAccessDeniedHandlerExceptionHandler securityAccessDeniedHandlerExceptionHandler) {
        this.jwtSecurityFilter = jwtSecurityFilter;
        this.securityAuthenticationEntryPointExceptionHandler = securityAuthenticationEntryPointExceptionHandler;
        this.securityAccessDeniedHandlerExceptionHandler = securityAccessDeniedHandlerExceptionHandler;
    }



    // 自定义加密方法
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /*
     * 认证
     * */
    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /*
     * 访问路径拦截
     * */
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                .csrf().disable()  // 关闭csrf
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)  // 不通过session获取SecurityContext
                .and()
                .authorizeRequests()
                .antMatchers("/user/login").anonymous()  // 允许登录接口匿名访问
                .antMatchers("/user/admin_manage/**").hasAuthority("sysAdmin")  // 需要ADMIN角色的路径
                .antMatchers("/user/manage/**").hasAnyAuthority("sysAdmin","manage")
                .antMatchers("/user/**").hasAnyAuthority("sysAdmin","manage","user")
                .antMatchers("/swagger-ui.html", "/v2/api-docs", "/webjars/**", "/swagger-resources/**").permitAll()
                .anyRequest().authenticated();  // 除上述之外的全部请求都需要鉴权认证

        http    // 将自定义JWT校验过滤链方法UsernamePasswordAuthenticationToken过滤链之前
                .addFilterBefore(jwtSecurityFilter, UsernamePasswordAuthenticationFilter.class);

        http
                .exceptionHandling()
                .authenticationEntryPoint(securityAuthenticationEntryPointExceptionHandler)  // 认证失败处理器
                .accessDeniedHandler(securityAccessDeniedHandlerExceptionHandler);  // 权限校验处理器

    }

}